//
//  AddLocationController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/11.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "AddLocationController.h"
#import "AddressPickerController.h"

@interface AddLocationController ()<AddressPickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *AddBackView;
@property (weak, nonatomic) IBOutlet UITextField *UserName;
@property (weak, nonatomic) IBOutlet UITextField *UseriPhone;
@property (weak, nonatomic) IBOutlet UILabel *Location01;
@property (weak, nonatomic) IBOutlet UITextField *Location02;
@property (weak, nonatomic) IBOutlet UIButton *IsFirstLocation;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation AddLocationController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"收货地址";
    
    UIButton * leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"MainBackImage_W"] forState:UIControlStateNormal];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(5,0,5,20);
    [leftBtn addTarget:self action:@selector(ClickLeftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,KScreenW,KScreenH/2);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:109/255.0 blue:1/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:41/255.0 blue:67/255.0 alpha:1].CGColor];
    gl.locations = @[@(0.0),@(1.0f)];
    [self.AddBackView.layer addSublayer:gl];
    
    self.UseriPhone.keyboardType = UIKeyboardTypeNumberPad;
    [self.IsFirstLocation setImage:[UIImage imageNamed:@"XFAddressImage_None"] forState:UIControlStateNormal];
    [self.IsFirstLocation setImage:[UIImage imageNamed:@"XFAddressImage_Select"] forState:UIControlStateSelected];
    self.IsFirstLocation.selected = NO;
}
- (IBAction)ClickOnBtnToChooseLocation:(id)sender {
    
    AddressPickerController * AddressC = [[AddressPickerController alloc] init];
    AddressC.delegate = self;
    AddressC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:AddressC animated:YES completion:nil];
}

- (void)ClickOnBtnToUploadChooseAddressString:(NSString *)AddressStr{
    if (AddressStr.length > 0) {
        self.Location01.text = AddressStr;
    }
}

- (IBAction)ClickOnBtnToChangeIsFirstbtn:(id)sender {
    self.IsFirstLocation.selected = !self.IsFirstLocation.selected;
}

- (IBAction)ClickOnBtnToAddTheNewLocation:(id)sender {
    if (self.UserName.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"收货人姓名不能为空"];
        return;
    }
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhone.text]) {
        [MBProgressHUD showAlertViewWithText:@"请填写正确的手机号"];
        return;
    }
    
    if (self.Location01.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择收货地址"];
        return;
    }
    if (self.Location02.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写详细地址"];
        return;
    }
    
    self.hud = [MainObjectWay CreateHUDView:self.view];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUserID forKey:@"address_member"];
    [dic setObject:self.UserName.text forKey:@"address_name"];
    [dic setObject:self.UseriPhone.text forKey:@"address_phone"];
    [dic setObject:self.Location01.text forKey:@"address_diqu"];
    [dic setObject:self.Location02.text forKey:@"address_xx"];
    if (self.IsFirstLocation.selected) {
        [dic setObject:@"1" forKey:@"address_default"];
    }else{
        [dic setObject:@"0" forKey:@"address_default"];
    }
    
    [MainObjectWay PostWithMethod:@"add_address" andAction:@"Pos" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"提交返回结果 = %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [MBProgressHUD showAlertViewWithText:@"添加成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
        }
        [self.hud hideAnimated:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"错误信息= %@",error);
        [self.hud hideAnimated:YES];
        [MBProgressHUD showAlertViewWithText:@"添加失败"];
    }];
    
    
}

- (void)ClickLeftBarButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
